package com.bulkmail.sender;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MimeMailMail {
	private JavaMailSender mailSender;
	private SimpleMailMessage simpleMailMessage;

	public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
		this.simpleMailMessage = simpleMailMessage;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void sendMail(String locationPath, boolean hasAttach, DataSession dataSession, String mailNb, String secure) {

		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			
			
			
			helper.setFrom(dataSession.getFrom());
			helper.setTo((String[])dataSession.getTo().toArray(new String[]{}));
			helper.setSubject(mailNb+" "+dataSession.getSubject()+" "+secure);
			helper.setText(dataSession.getMessage());
			

			if (hasAttach) {
				FileSystemResource file = new FileSystemResource(locationPath);
				helper.addAttachment(file.getFilename(), file);
			}

		} catch (MessagingException e) {
			throw new MailParseException(e);
		}
		mailSender.send(message);

	}

}